<?php
/**
 * 到账消息队列模型.
 * User: wuhao
 * Date: 2017/4/21
 * Time: 11:33
 */
namespace app\backend\model;

use think\Model;

class IncomeNotice extends Model
{
    private $_incomeNotice = 'z_income_notice_list';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 批量添加到账消息队列
     * @date   2017-04-21
     * @author wuhao
     *
     * @param  array  $data   数据
     * @return array
     */
    public function add($data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        return $this->setTable($this->_incomeNotice)->insertAll($data);
    }

    /**
     * 获取到账消息队列
     * @date   2017-4-26
     * @author wuhao
     *
     * @param  int    $ordernum      订单号
     * @param  int    $beginCycle    开始周期 20170501
     * @param  int    $endCycle      结束周期
     * @param  int    $notifyMethod  通知方式 1=短信 2=邮件 3=系统弹窗 0=所有
     * @param  int    $messageType   消息类型  1: 公告 2: 提醒 3：信息 0=所有
     * @param  int    $sendStatus    发送状态  0未发送  1已发送
     * @return array
     */
    public function getList($ordernum, $beginCycle, $endCycle, $notifyMethod, $messageType, $sendStatus)
    {
        if (!$ordernum || !$beginCycle || !$endCycle || !in_array($notifyMethod, [0,1,2,3]) || !in_array($messageType, [0,1,2,3]) || !in_array($sendStatus, [0,1])) {
            return false;
        }
        $where['ordernum']    = $ordernum;
        $where['begin_cycle'] = $beginCycle;
        $where['end_cycle']   = $endCycle;
        $where['send_status'] = $sendStatus;
        if ($notifyMethod) {
            $where['notify_method'] = $notifyMethod;
        }
        if ($messageType) {
            $where['message_type']  = $messageType;
        }
        $res = $this->setTable($this->_incomeNotice)->where($where)->select();
        return $res ?: [];
    }

    /**
     * 更新到账消息发送状态
     * @date   2017-4-26
     * @author wuhao
     *
     * @param  int|array  $id         id或id数组
     * @param  int        $sendStatus 发送状态 1=发送成功
     * @param  int        $sendTime   发送时间
     * @return bool
     */
    public function updateSendStatus($id, $sendStatus = 1, $sendTime)
    {
        if (!$id || (!is_numeric($id) && !is_array($id)) || !in_array($sendStatus, [0,1]) || !$sendTime) {
            return false;
        }
        if (is_array($id)) {
            $where = ['id' => ['in', $id]];
        } else {
            $where = ['id' => $id];
        }
        $data = [
            'send_status' => $sendStatus,
            'send_time'   => $sendTime,
            'update_time' => time(),
        ];
        return $this->setTable($this->_incomeNotice)->where($where)->update($data);
    }

    /**
     * 短信管理-获取到账短信列表
     * @date   2017-06-15
     * @author wuhao
     *
     * @return array
     */
    public function getSendList($begin, $end, $orderNumArr, $mobile, $notifyMethod, $field = '*', $page = 1, $pagSize = 15)
    {
        if (!$begin || !$end || !$orderNumArr) {
            return false;
        }
        if ($mobile) {
            $where['mobile'] = $mobile;
        }
        $where['ordernum'] = ['in', $orderNumArr];
        $where['notify_method'] = $notifyMethod;
        $where['send_status'] = 1;
        $where['send_time'] = ['between', [$begin, $end]];
        $res =  $this->table($this->_incomeNotice)->field($field)->where($where)->page($page, $pagSize)->select();
        if ($res) {
            $sum = $this->table($this->_incomeNotice)->where($where)->count();
            $total = ceil($sum / $pagSize);
            return [
                'total' => $total,
                'list'  => $res,
            ];
        } else {
            return [];
        }
    }
}